package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsArticleLike;
import com.uyayo.cms.mapper.CmsArticleLikeMapper;
import com.uyayo.cms.model.CmsArticleLikeCondition;
import com.uyayo.cms.service.CmsArticleLikeService;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("cmsArticleLikeService")
public class CmsArticleLikeServiceImpl extends BaseServiceImpl<CmsArticleLike> implements CmsArticleLikeService {

	private final CmsArticleLikeMapper cmsArticleLikeMapper;

	public CmsArticleLikeServiceImpl(CmsArticleLikeMapper cmsArticleLikeMapper) {
		this.cmsArticleLikeMapper = cmsArticleLikeMapper;
	}

	public List<CmsArticleLike> selectCmsArticleLike(CmsArticleLikeCondition cmsArticleLikeCondition) {
		Example example = new Example(CmsArticleLike.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(cmsArticleLikeCondition);
		example.createCriteria();
		return cmsArticleLikeMapper.selectByExample(example);
	}

	@Override
	public PageBean<CmsArticleLike> selectArticleLikeByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsArticleLikeCondition cmsArticleLikeCondition) {

		List<CmsArticleLike> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);

		Example example = new Example(CmsArticleLike.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Example.Criteria criteria = example.createCriteria();
		if(cmsArticleLikeCondition.getBeginCreateTime()!=null || cmsArticleLikeCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", cmsArticleLikeCondition.getBeginCreateTime(), cmsArticleLikeCondition.getEndCreateTime());
		}
		if(Objects.nonNull(cmsArticleLikeCondition.getStatus())){
			criteria.andEqualTo("status",cmsArticleLikeCondition.getStatus());
		}
		if(Objects.nonNull(cmsArticleLikeCondition.getArticleId())){
			criteria.andEqualTo("articleId",cmsArticleLikeCondition.getArticleId());
		}
		if(Objects.nonNull(cmsArticleLikeCondition.getMemberId())){
			criteria.andEqualTo("memberId",cmsArticleLikeCondition.getMemberId());
		}
		if(Objects.nonNull(cmsArticleLikeCondition.getLikeType())){
			criteria.andEqualTo("likeType",cmsArticleLikeCondition.getLikeType());
		}
		userList = cmsArticleLikeMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsArticleLike> pageInfo = new PageInfo<>(userList);

		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}
}
